package com.ysd.lis.service.inv.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.*;
import com.ysd.lis.mapper.inv.InvReagentStockMapper;
import com.ysd.lis.request.inv.FindReagentStockByBatchParam;
import com.ysd.lis.request.inv.InvReagentStockExpiryParam;
import com.ysd.lis.request.inv.InvReagentStockParam;
import com.ysd.lis.service.inv.InvReagentStockService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单信息 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-09-21
 */
@Service
public class InvReagentStockServiceImpl extends ServiceImpl<InvReagentStockMapper, InvReagentStock> implements InvReagentStockService {

    @Autowired
    InvReagentStockMapper invReagentStockMapper;

    @Override
    public Result findReagentStockList(InvReagentStockParam param) {

        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(InvReagentStock.class).selectAs("r", InvReagent::getReaNa, InvReagentStock::getReaNa).selectAs("r", InvReagent::getSpecModel, InvReagentStock::getSpecModel).selectAs("r", InvReagent::getExpirDay, InvReagentStock::getExpirDay).selectAs("ma", InvManufacturer::getManuName, InvReagentStock::getManufacturerName).selectAs("ism", InvStoreManage::getStoreName, InvReagentStock::getStoreName).leftJoin(InvReagent.class, "r", p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0)).leftJoin(InvManufacturer.class, "ma", p -> p.eq(InvManufacturer::getManuCode, InvReagentStock::getManufacturerNo).eq(InvManufacturer::getDelFlag, 0)).leftJoin(InvStoreManage.class, "ism", p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0))

                .eq(InvReagentStock::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvReagentStock::getReaNo, param.getSearchValue()).or().like(InvReagentStock::getBatchNum, param.getSearchValue()).or().like(InvReagent::getReaNa, param.getSearchValue()));
        }
        if (ToolsUtils.isNotEmpty(param.getStoreCode())) {
            lambdaQueryWrapper.eq(InvReagentStock::getStoreCode, param.getStoreCode());
        }
        if (ToolsUtils.isNotEmpty(param.getReaNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getReaNo, param.getReaNo());
        }
        if (ToolsUtils.isNotEmpty(param.getManufacturerNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getManufacturerNo, param.getManufacturerNo());
        }
        //库存为0不显示
        if (ToolsUtils.isNotEmpty(param.getQuery0Flag()) && param.getQuery0Flag()) {
            lambdaQueryWrapper.gt(InvReagentStock::getQuantity, 0);
        }
        lambdaQueryWrapper.orderByAsc(InvReagentStock::getReaNo).orderByDesc(InvReagentStock::getCreateTime);
        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);

        return Result.succ(1, "查询成功", materialList);

    }

    @Override
    public Result findReagentStockExpiryList(InvReagentStockExpiryParam param) {

        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(InvReagentStock.class).selectAs("r", InvReagent::getReaNa, InvReagentStock::getReaNa).selectAs("r", InvReagent::getSpecModel, InvReagentStock::getSpecModel).selectAs("r", InvReagent::getExpirDay, InvReagentStock::getExpirDay).selectAs("ma", InvManufacturer::getManuName, InvReagentStock::getManufacturerName).selectAs("ism", InvStoreManage::getStoreName, InvReagentStock::getStoreName).leftJoin(InvReagent.class, "r", p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0)).leftJoin(InvManufacturer.class, "ma", p -> p.eq(InvManufacturer::getManuCode, InvReagentStock::getManufacturerNo).eq(InvManufacturer::getDelFlag, 0)).leftJoin(InvStoreManage.class, "ism", p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0)).eq(InvReagentStock::getDelFlag, 0);

        lambdaQueryWrapper.le(InvReagentStock::getExpiryDate, param.getExpiryDate());

        lambdaQueryWrapper.orderByAsc(InvReagentStock::getReaNo).orderByDesc(InvReagentStock::getCreateTime);
        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);

        return Result.succ(1, "查询成功", materialList);

    }


    @Override
    public Result findHighStorageList(InvReagentStockParam param) {
        if (ToolsUtils.isEmpty(param.getStoreCode())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAs(InvReagentStock::getReaNo, InvReagentStock::getReaNo).selectAs(InvReagentStock::getUnit, InvReagentStock::getUnit).selectAs(InvReagent::getReaNa, InvReagentStock::getReaNa).selectAs(InvReagent::getSpecModel, InvReagentStock::getSpecModel).selectAs(InvReagentCategorize::getCategorizeName, InvReagentStock::getCategorizeName).selectAs(InvReagent::getMaxStock, InvReagentStock::getMaxStock).selectAs(InvStoreManage::getStoreName, InvReagentStock::getStoreName).selectSum(InvReagentStock::getQuantity, InvReagentStock::getQuantity).leftJoin(InvReagent.class, p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0)).leftJoin(InvStoreManage.class, p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0)).leftJoin(InvReagentCategorize.class, p -> p.eq(InvReagentCategorize::getId, InvReagent::getCategorizeId).eq(InvStoreManage::getDelFlag, 0)).eq(InvReagentStock::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(param.getStoreCode())) {
            lambdaQueryWrapper.eq(InvReagentStock::getStoreCode, param.getStoreCode());
        }
        if (ToolsUtils.isNotEmpty(param.getReaNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getReaNo, param.getReaNo());
        }
        lambdaQueryWrapper.groupBy(InvReagentStock::getReaNo);
        lambdaQueryWrapper.groupBy(InvReagentStock::getUnit);
        lambdaQueryWrapper.groupBy(InvReagent::getReaNa);
        lambdaQueryWrapper.groupBy(InvReagent::getSpecModel);
        lambdaQueryWrapper.groupBy(InvReagentCategorize::getCategorizeName);
        lambdaQueryWrapper.groupBy(InvReagent::getMaxStock);
        lambdaQueryWrapper.groupBy(InvStoreManage::getStoreName);

        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);
        List<InvReagentStock> resultList = new ArrayList<>();
        for (InvReagentStock invReagentStock : materialList) {
            if (Objects.nonNull(invReagentStock.getQuantity()) && invReagentStock.getQuantity() > 0 && Objects.nonNull(invReagentStock.getMaxStock())) {
                int result = Double.compare(invReagentStock.getMaxStock(), invReagentStock.getQuantity());
                if (result < 0) {
                    invReagentStock.setOverrunNum(invReagentStock.getQuantity() - invReagentStock.getMaxStock());
                    resultList.add(invReagentStock);
                }
            }
        }

        return Result.succ(1, "查询成功", resultList);
    }

    @Override
    public Result findLowStorageList(InvReagentStockParam param) {
        if (ToolsUtils.isEmpty(param.getStoreCode())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAs(InvReagentStock::getReaNo, InvReagentStock::getReaNo).selectAs(InvReagentStock::getUnit, InvReagentStock::getUnit).selectAs(InvReagent::getReaNa, InvReagentStock::getReaNa).selectAs(InvReagent::getSpecModel, InvReagentStock::getSpecModel).selectAs(InvReagentCategorize::getCategorizeName, InvReagentStock::getCategorizeName).selectAs(InvReagent::getMiniStock, InvReagentStock::getMiniStock).selectAs(InvStoreManage::getStoreName, InvReagentStock::getStoreName).selectSum(InvReagentStock::getQuantity, InvReagentStock::getQuantity).leftJoin(InvReagent.class, p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0)).leftJoin(InvStoreManage.class, p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0)).leftJoin(InvReagentCategorize.class, p -> p.eq(InvReagentCategorize::getId, InvReagent::getCategorizeId).eq(InvStoreManage::getDelFlag, 0)).eq(InvReagentStock::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(param.getStoreCode())) {
            lambdaQueryWrapper.eq(InvReagentStock::getStoreCode, param.getStoreCode());
        }
        if (ToolsUtils.isNotEmpty(param.getReaNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getReaNo, param.getReaNo());
        }
        lambdaQueryWrapper.groupBy(InvReagentStock::getReaNo);
        lambdaQueryWrapper.groupBy(InvReagentStock::getUnit);
        lambdaQueryWrapper.groupBy(InvReagent::getReaNa);
        lambdaQueryWrapper.groupBy(InvReagent::getSpecModel);
        lambdaQueryWrapper.groupBy(InvReagentCategorize::getCategorizeName);
        lambdaQueryWrapper.groupBy(InvReagent::getMiniStock);
        lambdaQueryWrapper.groupBy(InvStoreManage::getStoreName);

        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);
        List<InvReagentStock> resultList = new ArrayList<>();
        for (InvReagentStock invReagentStock : materialList) {
            if (Objects.nonNull(invReagentStock.getQuantity()) && invReagentStock.getQuantity() > 0 && Objects.nonNull(invReagentStock.getMiniStock())) {
                int result = Double.compare(invReagentStock.getMiniStock(), invReagentStock.getQuantity());
                if (result > 0) {
                    invReagentStock.setOverrunNum(invReagentStock.getMiniStock() - invReagentStock.getQuantity());
                    resultList.add(invReagentStock);
                }
            }
        }

        return Result.succ(1, "查询成功", resultList);
    }

    /*批次查询*/
    @Override
    public Result findReagentStockByBatch(FindReagentStockByBatchParam input) {

        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(InvReagentStock.class);
        lambdaQueryWrapper.selectAs("r", InvReagent::getReaNa, InvReagentStock::getReaNa);
        lambdaQueryWrapper.selectAs("r", InvReagent::getSpecModel, InvReagentStock::getSpecModel);
        lambdaQueryWrapper.selectAs("r", InvReagent::getExpirDay, InvReagentStock::getExpirDay);
        lambdaQueryWrapper.selectAs("ma", InvManufacturer::getManuName, InvReagentStock::getManufacturerName);
        lambdaQueryWrapper.selectAs("ism", InvStoreManage::getStoreName, InvReagentStock::getStoreName);
        lambdaQueryWrapper.leftJoin(InvReagent.class, "r", p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(InvManufacturer.class, "ma", p -> p.eq(InvManufacturer::getManuCode, InvReagentStock::getManufacturerNo).eq(InvManufacturer::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(InvStoreManage.class, "ism", p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0));
        lambdaQueryWrapper.eq(InvReagentStock::getDelFlag, 0);
        if (StringUtils.isNotBlank(input.getBatch())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvReagentStock::getBatchNum, input.getBatch()));
        }
        lambdaQueryWrapper.orderByAsc(InvReagentStock::getBatchNum).orderByDesc(InvReagentStock::getReaNo);
        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);

        return Result.succ(1, "查询成功", materialList);
    }

    @Override
    public Result findStockToprList(InvReagentStockParam param) {

        if (ToolsUtils.isEmpty(param.getStoreCode())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAs(InvReagentStock::getReaNo, InvReagentStock::getReaNo)
                .selectAs(InvReagentStock::getUnit, InvReagentStock::getUnit)
                .selectAs(InvReagent::getReaNa, InvReagentStock::getReaNa)
                .selectAs(InvReagent::getSpecModel, InvReagentStock::getSpecModel)
                .selectAs(InvReagentCategorize::getCategorizeName, InvReagentStock::getCategorizeName)
                .selectAs(InvReagent::getMiniStock, InvReagentStock::getMiniStock)
                .selectAs(InvStoreManage::getStoreName, InvReagentStock::getStoreName)
                .selectSum(InvReagentStock::getQuantity, InvReagentStock::getQuantity)
                .leftJoin(InvReagent.class, p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0))
                .leftJoin(InvStoreManage.class, p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0))
                .leftJoin(InvReagentCategorize.class, p -> p.eq(InvReagentCategorize::getId, InvReagent::getCategorizeId).eq(InvStoreManage::getDelFlag, 0)).eq(InvReagentStock::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(param.getStoreCode())) {
            lambdaQueryWrapper.eq(InvReagentStock::getStoreCode, param.getStoreCode());
        }
        if (ToolsUtils.isNotEmpty(param.getReaNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getReaNo, param.getReaNo());
        }
        lambdaQueryWrapper.gt(InvReagentStock::getQuantity, 0);

        lambdaQueryWrapper.groupBy(InvReagentStock::getReaNo);
        lambdaQueryWrapper.groupBy(InvReagentStock::getUnit);
        lambdaQueryWrapper.groupBy(InvReagent::getReaNa);
        lambdaQueryWrapper.groupBy(InvReagent::getSpecModel);
        lambdaQueryWrapper.groupBy(InvReagentCategorize::getCategorizeName);
        lambdaQueryWrapper.groupBy(InvReagent::getMiniStock);
        lambdaQueryWrapper.groupBy(InvStoreManage::getStoreName);
        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);
        if(materialList.size()>10){
            List<InvReagentStock> returnList = new ArrayList<>();
            List<InvReagentStock> collect = materialList.stream().sorted(Comparator.comparing(InvReagentStock::getQuantity).reversed()).collect(Collectors.toList());
            for(int i = 0 ; i< 10 ;i++){
                returnList.add(collect.get(i));
            }
            return Result.succ(1, "查询成功", returnList);
        }else {
            return Result.succ(1, "查询成功", materialList.stream().sorted(Comparator.comparing(InvReagentStock::getQuantity).reversed()).collect(Collectors.toList()));

        }

    }

    @Override
    public Result findStockExpiryList(InvReagentStockParam param) {
        if (ToolsUtils.isEmpty(param.getStoreCode())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        MPJLambdaWrapper<InvReagentStock> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAs(InvReagentStock::getReaNo, InvReagentStock::getReaNo)
                .selectAs(InvReagentStock::getUnit, InvReagentStock::getUnit)
                .selectAs(InvReagent::getReaNa, InvReagentStock::getReaNa)
                .selectAs(InvReagent::getSpecModel, InvReagentStock::getSpecModel)
                .selectAs(InvReagentCategorize::getCategorizeName, InvReagentStock::getCategorizeName)
                .selectAs(InvReagent::getMiniStock, InvReagentStock::getMiniStock)
                .selectAs(InvStoreManage::getStoreName, InvReagentStock::getStoreName)
                .selectSum(InvReagentStock::getQuantity, InvReagentStock::getQuantity)
                .leftJoin(InvReagent.class, p -> p.eq(InvReagent::getReaNo, InvReagentStock::getReaNo).eq(InvReagent::getDelFlag, 0))
                .leftJoin(InvStoreManage.class, p -> p.eq(InvStoreManage::getStoreCode, InvReagentStock::getStoreCode).eq(InvStoreManage::getDelFlag, 0))
                .leftJoin(InvReagentCategorize.class, p -> p.eq(InvReagentCategorize::getId, InvReagent::getCategorizeId).eq(InvStoreManage::getDelFlag, 0)).eq(InvReagentStock::getDelFlag, 0);
        if (ToolsUtils.isNotEmpty(param.getStoreCode())) {
            lambdaQueryWrapper.eq(InvReagentStock::getStoreCode, param.getStoreCode());
        }
        if (ToolsUtils.isNotEmpty(param.getReaNo())) {
            lambdaQueryWrapper.eq(InvReagentStock::getReaNo, param.getReaNo());
        }
        lambdaQueryWrapper.gt(InvReagentStock::getQuantity, 0);
        Calendar calendar = Calendar.getInstance(); // 获取当前日期的Calendar实例
        Date today = calendar.getTime();
        Timestamp timestamp = DateUtil.beginOfDay(today).toTimestamp();
        lambdaQueryWrapper.le(InvReagentStock::getExpiryDate, timestamp);

        lambdaQueryWrapper.groupBy(InvReagentStock::getReaNo);
        lambdaQueryWrapper.groupBy(InvReagentStock::getUnit);
        lambdaQueryWrapper.groupBy(InvReagent::getReaNa);
        lambdaQueryWrapper.groupBy(InvReagent::getSpecModel);
        lambdaQueryWrapper.groupBy(InvReagentCategorize::getCategorizeName);
        lambdaQueryWrapper.groupBy(InvReagent::getMiniStock);
        lambdaQueryWrapper.groupBy(InvStoreManage::getStoreName);
        List<InvReagentStock> materialList = invReagentStockMapper.selectList(lambdaQueryWrapper);
        if(materialList.size()>10){
            List<InvReagentStock> returnList = new ArrayList<>();
            List<InvReagentStock> collect = materialList.stream().sorted(Comparator.comparing(InvReagentStock::getQuantity).reversed()).collect(Collectors.toList());
            for(int i = 0 ; i< 10 ;i++){
                returnList.add(collect.get(i));
            }
            return Result.succ(1, "查询成功", returnList);
        }else {
            return Result.succ(1, "查询成功", materialList.stream().sorted(Comparator.comparing(InvReagentStock::getQuantity).reversed()).collect(Collectors.toList()));

        }
    }
}
